package com.bj58.ecdata.dashboard.controllers;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.bj58.ecdata.dashboard.cache.ServiceBeanFactory;
import com.bj58.ecdata.dashboard.constant.CityHomePageConst;
import com.bj58.ecdata.dashboard.constant.EnumCate;
import com.bj58.ecdata.dashboard.constant.IncomeCityConst;
import com.bj58.ecdata.dashboard.databll.HomePageBLL;
import com.bj58.ecdata.dashboard.databll.PriviledgeBLL;
import com.bj58.ecdata.dashboard.dbentity.HomePageNetflow;
import com.bj58.ecdata.dashboard.dbentity.ConsumeBalance;
import com.bj58.ecdata.dashboard.service.AbstractBaseService;
import com.bj58.ecdata.dashboard.utils.DateUtil;
import com.bj58.ecdata.dashboard.utils.MyServletRequestUtils;
import com.bj58.groupbuy.action.model.User;
import com.bj58.groupbuy.action.util.SomePublicUtil;
import com.bj58.wf.mvc.ActionResult;
import com.bj58.wf.mvc.BeatContext.Model;
import com.bj58.wf.mvc.MvcController;
import com.bj58.wf.mvc.annotation.GET;
import com.bj58.wf.mvc.annotation.Path;

@Path("/homePage")
public class HomePageController extends MvcController{
	private SomePublicUtil spu = new SomePublicUtil();
	private static AbstractBaseService<HomePageNetflow> cityNetflowService = ServiceBeanFactory.get(HomePageNetflow.class,null);
	private static AbstractBaseService<ConsumeBalance> ConsumeBalanceService = ServiceBeanFactory.get(ConsumeBalance.class, null);
	
	/**
	 *城市首页看板
	 * @return
	 * @author zhaoxiang 2015-11-30
	 */
	@Path({ "/income" })
//	@Check("dashboard/homePage/income")
	@GET
	public ActionResult income() {
		try {
			HttpServletRequest request = beat.getRequest();
			User user = spu.getUserByCookie(request);
			//检查业务线权限
			String businessOperStr = PriviledgeBLL.businessOperCheck(user);
			String[] businessOpers = businessOperStr.split(",");

			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String cityName = MyServletRequestUtils.getStringParameter(request, "cityName", "北京");

			if ("total".equals(businessOpers[0])) {
				businessOpers = Arrays.copyOfRange(businessOpers, 1, businessOpers.length);
			}
			if(StringUtils.isBlank(businessName))
				businessName = EnumCate.business_CN_Map.getString(businessOpers[0]);
			
			Model model = beat.getModel();
			// 业务线 中文名
			model.add("businessName", businessName);
			model.add("cityName", cityName);
			model.add("business_CN_Map", EnumCate.business_CN_Map);
			// 业务线权限
			model.add("businessList", businessOpers);
			model.add("zhixiaoCityList", IncomeCityConst.ZX_CityList); // 直销城市
			model.add("indexList_EN", CityHomePageConst.indexList_EN); // 指标
			model.add("indexList_CN", CityHomePageConst.indexList_CN); // 中文显示指标
			model.add("cateList", CityHomePageConst.getCateListByBusiness(businessName)); // 重点类别，下拉选择
		} catch (Exception e) {
			log.error(null, e);
			return ActionResult.view("null");
		}
		return ActionResult.view("homePage/income");
	}
	
	/**
	 *城市首页看板
	 * @return
	 * @author zhaoxiang 2015-11-30
	 */
	@Path({ "/netflow" })
//	@Check("dashboard/homePage/netflow")
	@GET
	public ActionResult netflow() {
		try {
			HttpServletRequest request = beat.getRequest();
			User user = spu.getUserByCookie(request);
			//检查业务线权限
			String businessOperStr = PriviledgeBLL.businessOperCheck(user);
			String[] businessOpers = businessOperStr.split(",");

			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "招聘");
			String cityName = MyServletRequestUtils.getStringParameter(request, "city", "北京");

			if ("total".equals(businessOpers[0])) {
				businessOpers = Arrays.copyOfRange(businessOpers, 1, businessOpers.length);
			}
			if(StringUtils.isBlank(businessName))
				businessName = EnumCate.business_CN_Map.getString(businessOpers[0]);
			
			Model model = beat.getModel();
			// 整体流量
			String maxDate = cityNetflowService.getMaxDate(String.format("business_name='%s' and stat_type='day'", businessName));
			List<List<Object>> list = HomePageBLL.getGeneral(maxDate, cityName, businessName);
			model.add("yesterdayTable", list);
			model.add("stat_date", DateUtil.strToStr(maxDate, "yyyy.MM.dd"));
			// 业务线 中文名
			model.add("businessName", businessName);
			model.add("city", cityName);
			model.add("business_CN_Map", EnumCate.business_CN_Map);
			// 业务线权限
			model.add("businessList", businessOpers);
			model.add("zhixiaoCityList", IncomeCityConst.ZX_CityList); // 直销城市
			model.add("indexList_EN", CityHomePageConst.indexList_EN); // 指标
			model.add("indexList_CN", CityHomePageConst.indexList_CN); // 中文显示指标
			model.add("cateList", CityHomePageConst.getCateListByBusiness(businessName)); // 重点类别，下拉选择
		} catch (Exception e) {
			log.error(null, e);
			return ActionResult.view("null");
		}
		return ActionResult.view("homePage/netflow");
	}
	
	/**
	 *城市首页看板
	 * @return
	 * @author guoxue 2015-12-28
	 */
	@Path({ "/info" })
//	@Check("dashboard/homePage/info")
	@GET
	public ActionResult info() {
		try {
			HttpServletRequest request = beat.getRequest();
			User user = spu.getUserByCookie(request);
			//检查业务线权限
			String businessOperStr = PriviledgeBLL.businessOperCheck(user);
			String[] businessOpers = businessOperStr.split(",");

			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "招聘");
			String cityName = MyServletRequestUtils.getStringParameter(request, "cityName", "北京");

			if ("total".equals(businessOpers[0])) {
				businessOpers = Arrays.copyOfRange(businessOpers, 1, businessOpers.length);
			}
			if(StringUtils.isBlank(businessName))
				businessName = EnumCate.business_CN_Map.getString(businessOpers[0]);
			
			Model model = beat.getModel();
			
			// 业务线 中文名
			model.add("businessName", businessName);
			model.add("cityName", cityName);
			model.add("business_CN_Map", EnumCate.business_CN_Map);
			// 业务线权限
			model.add("businessList", businessOpers);
			model.add("zhixiaoCityList", IncomeCityConst.ZX_CityList); // 直销城市
			model.add("indexList_EN", CityHomePageConst.indexList_EN); // 指标
			model.add("indexList_CN", CityHomePageConst.indexList_CN); // 中文显示指标
			model.add("cateList", CityHomePageConst.getCateListByBusiness(businessName)); // 重点类别，下拉选择
		} catch (Exception e) {
			log.error(null, e);
			return ActionResult.view("null");
		}
		return ActionResult.view("homePage/info");
	}
	
	/**
	 *城市首页看板
	 * @return
	 * @author wangchonglu 2016-01-12
	 */
	@Path({ "/inventory" })
//	@Check("dashboard/homePage/inventory")
	@GET
	public ActionResult inventory() {
		try {
			HttpServletRequest request = beat.getRequest();
			User user = spu.getUserByCookie(request);
			//检查业务线权限
			String businessOperStr = PriviledgeBLL.businessOperCheck(user);
			String[] businessOpers = businessOperStr.split(",");

			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "总体");
			String cityName = MyServletRequestUtils.getStringParameter(request, "cityName", "北京");

			if ("total".equals(businessOpers[0])) {
				businessOpers = Arrays.copyOfRange(businessOpers, 1, businessOpers.length);
			}
			if(StringUtils.isBlank(businessName))
				businessName = EnumCate.business_CN_Map.getString(businessOpers[0]);
			
			Model model = beat.getModel();
			
			// 业务线 中文名
			model.add("businessName", businessName);
			model.add("cityName", cityName);
			model.add("business_CN_Map", EnumCate.business_CN_Map);
			// 业务线权限
			model.add("businessList", businessOpers);
			model.add("zhixiaoCityList", IncomeCityConst.ZX_CityList); // 直销城市
			model.add("targetList", CityHomePageConst.targetList); // 指标
			model.add("cateList", CityHomePageConst.getCateListByBusiness(businessName)); // 重点类别，下拉选择
			
			String maxDate = ConsumeBalanceService.getMaxDate("stat_type='month' and business_name='"+businessName+"' ");
			maxDate=maxDate.substring(0,4)+maxDate.substring(5,7);
			
			model.add("maxDate_consumeRate", maxDate);
			
		} catch (Exception e) {
			log.error(null, e);
			return ActionResult.view("null");
		}
		return ActionResult.view("homePage/inventory");
	}
	
}
